package cn.woblog;

import java.io.File;

public class Main {
    private long time=0;

    public static void main(String[] args) {
//        File file = new File("/Users/smile/Documents/works/temp/3.如何查看Gradle依赖库最新版本.mp4");
//        long videoTime = VideoUtil.getVideoTime(file);
//        System.out.println(videoTime);

//        VideoUtil.getVideoTime(file.getAbsolutePath());

        if (args.length != 2) {
            System.out.println("Usage: ffmpegPath dir");
            return;
        }

        new Main().start(args[0].trim(),args[1].trim());
    }

    private  void start(String ffmpegPath, String dir) {
        File ffmpegPathFile = new File(ffmpegPath);

        if (!ffmpegPathFile.exists()) {
            System.out.println("ffmpegPath not found.");
            return;
        }

        File dirFile = new File(dir);

        if (!dirFile.exists()) {
            System.out.println("dir not found.");
            return;
        }


        VideoUtil.FFMPEG_PATH=ffmpegPathFile.getAbsolutePath();

        process(dirFile);

        System.out.println("时间："+(time/60)+"分钟");
    }

    private  void process(File rootFile) {

        loopOutAllFileName(rootFile);
    }

    private void loopOutAllFileName(File testFileDir) {
        if (testFileDir == null) {
            //因为new File(null)会空指针异常,所以要判断下
            return;
        }
        File[] testFile = testFileDir.listFiles();
        if (testFile == null) {
            return;
        }
        for (File file : testFile) {
            if (file.isFile()) {
                if (file.getName().endsWith(".mp4")) {
                    System.out.println(file.getName());

                    //计算时间
                    time+=VideoUtil.getVideoTime(file.getAbsolutePath());
                }

            } else if (file.isDirectory()) {
//                System.out.println("-------this is a directory, and its files are as follows:-------");
                loopOutAllFileName(file);
            } else {
                System.out.println("文件读入有误！");
            }
        }
    }


}
